package SERVLET;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import BO.ListaBO;
import BO.ObraBO;
import BO.StatusBO;
import BO.UsuarioBO;
import DTO.ListaDTO;
import DTO.ObraDTO;
import DTO.UsuarioDTO;

public class ListaServlet extends HttpServlet {
        
	private ListaDTO listaDTO = new ListaDTO();
	private ListaBO listaBO = new ListaBO();
	private UsuarioBO usuarioBO = new UsuarioBO();
	private ObraBO obraBO = new ObraBO();
	private StatusBO statusBO = new StatusBO();
	
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-control", "no-cache, must-revalidate");
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Expires", "Fri, 25 Dec 1980 00:00:00 GMT");

        if (request.getParameter("acao") != null) {
        	if (request.getParameter("acao").equals("MinhaLista") || (request.getParameter("acao").equals("VerLista"))){
        		verLista(request, response);        	
        	} else if(request.getParameter("acao").equals("Adicionar")){
        		adicionar(request, response);
        	} else if(request.getParameter("acao").equals("EditarLista")){
        		editar(request, response);        	
            } else {
                redicionarPesquisar(request, response);
            }
        }
    }           
    
    private void editar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	listaDTO.setObra(obraBO.pesquisarPorCodigo(Long.parseLong(request.getParameter("idObra"))));
    	listaDTO.setUsuario(usuarioBO.pesquisarPorCodigo(Long.parseLong(request.getParameter("idUsuario"))));
    	listaDTO.setStatus(statusBO.pesquisarPorCodigo(Long.parseLong(request.getParameter("status"))));
    	listaBO.alterar(listaDTO);    	
    	request.getSession().setAttribute("lista", listaBO.pesquisarObrasDoUsuario(listaDTO.getUsuario()));
    	redicionarPesquisar(request, response);
    }
       
    private void verLista(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	UsuarioDTO usuario = usuarioBO.pesquisarPorCodigo(Long.parseLong(request.getParameter("id")));
    	request.getSession().setAttribute("usuario", usuario);
    	request.getSession().setAttribute("lista", listaBO.pesquisarObrasDoUsuario(usuario));
    	redicionarPesquisar(request, response);
    }
    
    private void adicionar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	listaDTO.setObra((ObraDTO) request.getSession().getAttribute("obraDTO"));
    	listaDTO.setUsuario((UsuarioDTO) request.getSession().getAttribute("usuarioLogado"));
    	listaDTO.setStatus(statusBO.pesquisarPorCodigo(Long.parseLong(request.getParameter("status"))));
    	listaBO.incluir(listaDTO);
		request.getSession().setAttribute("lista", listaBO.pesquisarObrasDoUsuario(listaDTO.getUsuario()));
    	request.getSession().setAttribute("usuario", listaDTO.getUsuario());  
    	redicionarPesquisar(request, response);
    }
    
    private void redicionarPesquisar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{    	
        request.getRequestDispatcher(HomeServlet.paginas.get("lista")).forward(request, response);
    }   
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }
       
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}